//
//  WmcDrawTitleContentItem.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/7.
//

import SnapKit
import UIKit
import YPBedrock

extension WmcDrawTitleContentItem {
    /// 标题类型
    enum TitleStyle {
        /// 无标题
        case noTitle
        /// 标题间距对齐
        case title(_ title: String,
                   maxTitleCount: Int)
        /// 带前缀图，但是无标题
        case prefixImage(_ prefixImage: UIImage,
                         prefixImageWidth: CGFloat = 10)
        /// 带前缀图，带标题
        case prefixImageTitle(_ prefixImage: UIImage,
                              prefixImageWidth: CGFloat = 10,
                              title: String,
                              maxTitleCount: Int)
    }

    /// 内容类型
    enum ContentType {
        /// 时间
        case time(format: WmcDateLabel.Format)
        /// 地点
        case location(format: WmcLocationLabel.Format)
        /// 天气
        case weather(format: WmcWeatherLabel.Format)
        /// 海拔
        case altitude(format: WmcAltitudeLabel.Format)
        /// 方位角
        case azimuthAngle(format: WmcAzimuthAngleLabel.Format)
        /// 分贝
        case decibe(format: WmcDecibeLabel.Format)
        /// 速度
        case speed(format: WmcSpeedLabel.Format)
        /// 文字
        case content(text: String)
    }

    /// 初始化配置对象
    struct Config {
        var titleStyle: TitleStyle
        var font: YPUnifiedFont = 14
        var color: YPUnifiedColor = UIColor.white
        var contentType: WmcDrawTitleContentItem.ContentType
        var leftEdge: CGFloat = 8
    }
}

// MARK: - [WmcDrawTitleContentItem]

class WmcDrawTitleContentItem: WmcBaseView {
    // Lifecycle

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    convenience init(config: Config) {
        self.init(frame: .zero)
        self.config = config
        self._makeUI()
    }

    // Internal

    /// 内容类型
    var config: Config!

    /// 时间
    lazy var timeLabel: WmcDateLabel = {
        let label = WmcDateLabel.YP.Create(font: 14.wmc_font,
                                           color: UIColor.white,
                                           alignment: .left)
        label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        label.leftEdge = 0
        label.numberOfLines = 0
        return label
    }()

    /// 地点
    lazy var locationLabel: WmcLocationLabel = {
        let label = WmcLocationLabel.YP.Create(font: 14.wmc_font,
                                               color: UIColor.white,
                                               alignment: .left)
        label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        label.leftEdge = 0
        label.numberOfLines = 0
        return label
    }()

    /// 天气
    lazy var weatherLabel: WmcWeatherLabel = {
        let label = WmcWeatherLabel.YP.Create(font: 14.wmc_font,
                                              color: UIColor.white,
                                              alignment: .left)
        label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        label.leftEdge = 0
        label.numberOfLines = 0
        return label
    }()

    /// 海拔
    lazy var altitudeLabel: WmcAltitudeLabel = {
        let label = WmcAltitudeLabel.YP.Create(font: 14.wmc_font,
                                               color: UIColor.white,
                                               alignment: .left)
        label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        label.leftEdge = 0
        label.numberOfLines = 0
        return label
    }()

    /// 方位角
    lazy var azimuthAngleLabel: WmcAzimuthAngleLabel = {
        let label = WmcAzimuthAngleLabel.YP.Create(font: 14.wmc_font,
                                                   color: UIColor.white,
                                                   alignment: .left)
        label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        label.leftEdge = 0
        label.numberOfLines = 0
        return label
    }()

    /// 分贝
    lazy var decibeLabel: WmcDecibeLabel = {
        let label = WmcDecibeLabel.YP.Create(font: 14.wmc_font,
                                             color: UIColor.white,
                                             alignment: .left)
        label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        label.leftEdge = 0
        label.numberOfLines = 0
        return label
    }()

    /// 速度
    lazy var speedLabel: WmcSpeedLabel = {
        let label = WmcSpeedLabel.YP.Create(font: 14.wmc_font,
                                            color: UIColor.white,
                                            alignment: .left)
        label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        label.leftEdge = 0
        label.numberOfLines = 0
        return label
    }()

    /// 文字内容
    lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        return label
    }()

    /// 标题
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
        return label
    }()

    /// 设置内容
    func setContent() {
        // 设置标题
        switch config.titleStyle {
        case .noTitle:
            break
        case let .title(title, maxTitleCount: maxTitleCount):
            setTitle(title, maxTitleCount: maxTitleCount, font: config.font, color: config.color)

        case let .prefixImage(prefixImage, prefixImageWidth: prefixImageWidth):
            setPrefixImage(prefixImage, prefixImageWidth: prefixImageWidth)

        case let .prefixImageTitle(prefixImage, prefixImageWidth: prefixImageWidth, title: title, maxTitleCount: maxTitleCount):
            setTitle(title, maxTitleCount: maxTitleCount, font: config.font, color: config.color)
            setPrefixImage(prefixImage, prefixImageWidth: prefixImageWidth)
        }
        // 设置内容
        switch config.contentType {
        case let .time(format: format):
            timeLabel.format = format

        case let .location(format: format):
            locationLabel.format = format

        case let .weather(format: format):
            weatherLabel.format = format

        case let .azimuthAngle(format: format):
            azimuthAngleLabel.format = format

        case let .altitude(format: format):
            altitudeLabel.format = format

        case let .decibe(format: format):
            decibeLabel.format = format

        case let .speed(format: format):
            speedLabel.format = format

        case let .content(text: text):
            contentLabel.text = text
        }
        // 设置字体
        setContentFont(config.font)
        // 设置字色
        setContentColor(config.color)
    }

    /// 设置图片前缀
    func setPrefixImage(_ image: UIImage,
                        prefixImageWidth: CGFloat)
    {
        prefixImageView.image = image
        prefixImageView.snp.updateConstraints { make in
            make.width.equalTo(prefixImageWidth)
        }
    }

    /// 设置标题
    func setTitle(_ title: String,
                  maxTitleCount: Int,
                  font: YPUnifiedFont,
                  color: YPUnifiedColor)
    {
        let font = UIFont.YP.Create(font).yp.uiFont(weight: .medium)
        let textColor = UIColor.YP.Create(color)
        var kern: CGFloat = 0
        let fontWidth: CGFloat = font.pointSize
        let maxTitleWdith: CGFloat = maxTitleCount.yp.cgFloat * fontWidth
        let count: CGFloat = title.count.yp.cgFloat
        if count >= 2, count < maxTitleCount.yp.cgFloat {
            kern = (maxTitleWdith - count * fontWidth) / (count - 1)
        }
        let attTitleText = title.yp.makeAttributed { make in
            let paragraphStyle = NSMutableParagraphStyle().yp.makeParagraphStyle { make in
                make.set(alignment:.left)
            }
            if count > 0 {
                let range = 0 ... (count - 1)
                make.set(font: font)
                    .set(foregroundColor:textColor)
                    .set(kern:kern, range: range)
                    .set(paragraphStyle:paragraphStyle)
            }
        }
        titleLabel.attributedText = attTitleText
        titleLabel.snp.updateConstraints { make in
            make.width.equalTo(maxTitleWdith)
        }
    }

    /// 设置字号
    func setContentFont(_ font: YPUnifiedFont) {
        let font = UIFont.YP.Create(font)
        switch config.titleStyle {
        case .title,
             .prefixImageTitle:
            contentLabel.font = font
            colonLabel.font = font
        default: break
        }
        switch config.contentType {
        case .time:
            timeLabel.font = font
        case .location:
            locationLabel.font = font
        case .weather:
            weatherLabel.font = font
        case .altitude:
            altitudeLabel.font = font
        case .azimuthAngle:
            azimuthAngleLabel.font = font
        case .decibe:
            decibeLabel.font = font
        case .speed:
            speedLabel.font = font

        case .content: break
        }
    }

    /// 设置内容颜色
    func setContentColor(_ color: YPUnifiedColor) {
        let textColor = UIColor.YP.Create(color)
        switch config.titleStyle {
        case .title:
            contentLabel.textColor = textColor
            colonLabel.textColor = textColor

        case .prefixImage:
            prefixImageView.yp.set(tintColor: textColor)

        case .prefixImageTitle:
            prefixImageView.yp.set(tintColor: textColor)
            contentLabel.textColor = textColor
            colonLabel.textColor = textColor
        default: break
        }
        switch config.contentType {
        case .time:
            timeLabel.textColor = textColor
        case .location:
            locationLabel.textColor = textColor
        case .weather:
            weatherLabel.textColor = textColor
        case .altitude:
            altitudeLabel.textColor = textColor
        case .azimuthAngle:
            azimuthAngleLabel.textColor = textColor
        case .decibe:
            decibeLabel.textColor = textColor
        case .speed:
            speedLabel.textColor = textColor

        case .content: break
        }
    }

    // Private

    private lazy var colonLabel: UILabel = {
        let label = UILabel.YP.Create(text: "：", font: 14, color: UIColor.white, alignment: .left)
        label.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
        return label
    }()

    private lazy var prefixImageView: UIImageView = {
        let imageView = UIImageView()
        return imageView
    }()

    // 布局
    private func _makeUI() {
        backgroundColor = .clear

        var prefixView: UIView?

        switch config.titleStyle {
        case .noTitle: break

        case .title:
            makeTitleView()
            prefixView = colonLabel

        case .prefixImage:
            makePrefixImageView()
            prefixView = prefixImageView

        case .prefixImageTitle:
            makePrefixImageView()
            makeTitleView() { [weak self] make in
                guard let weakSelf = self else { return }
                make.left.equalTo(weakSelf.prefixImageView.snp.right).offset(4)
            }
            prefixView = colonLabel
        }

        var _contentView: UIView?
        switch config.contentType {
        case let .time(format: format):
            timeLabel.format = format
            _contentView = timeLabel

        case let .location(format: format):
            locationLabel.format = format
            _contentView = locationLabel

        case let .weather(format: format):
            weatherLabel.format = format
            _contentView = weatherLabel

        case let .altitude(format: format):
            altitudeLabel.format = format
            _contentView = altitudeLabel

        case let .azimuthAngle(format: format):
            azimuthAngleLabel.format = format
            _contentView = azimuthAngleLabel

        case let .decibe(format: format):
            decibeLabel.format = format
            _contentView = decibeLabel

        case let .speed(format: format):
            speedLabel.format = format
            _contentView = speedLabel

        case let .content(text: text):
            contentLabel.text = text
            _contentView = contentLabel
        }
        guard let _contentView = _contentView else { return }
        addSubview(_contentView)
        makeContentView(_contentView, prefixView: prefixView)
        // 设置内容
        setContent()
    }

    // 布局前缀图片视图
    private func makePrefixImageView() {
        addSubview(prefixImageView)
        prefixImageView.snp.makeConstraints { make in
            make.top.equalTo(6)
            make.width.equalTo(10)
            make.left.equalTo(config.leftEdge)
        }
    }

    // 布局标题视图
    private func makeTitleView(makeBlock: YPBlock.OneParam<ConstraintMaker>? = nil) {
        addSubview(titleLabel)
        addSubview(colonLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(4)
            make.width.equalTo(10)
            if let block = makeBlock {
                block(make)
            } else {
                make.left.equalTo(config.leftEdge)
            }
        }
        colonLabel.snp.makeConstraints { make in
            make.top.bottom.equalTo(titleLabel)
            make.left.equalTo(titleLabel.snp.right)
        }
    }

    // 布局内容视图
    private func makeContentView(_ view: UIView, prefixView: UIView?) {
        view.snp.makeConstraints { make in
            make.top.equalTo(4)
            make.bottom.equalTo(-4)
            make.right.equalTo(-8)
            if let prefixView = prefixView {
                make.left.equalTo(prefixView.snp.right).offset(4)
            } else {
                make.left.equalTo(config.leftEdge)
            }
        }
    }
}
